<?php

function post($url, $post) {

    global $ch, $curlConfig;

    $curlC = $curlConfig;

    $curlC[CURLOPT_URL] = $url;
    $curlC[CURLOPT_POST] = true;
    $curlC[CURLOPT_POSTFIELDS] = $post;

    curl_setopt_array($ch, $curlC);

    $result = curl_exec($ch);

    if (curl_errno($ch)) {
        echo 'Błąd #' . curl_errno($ch) . ': ' . curl_error($ch);
        return;
    }

    return $result;
}

function get($url) {

    global $ch, $curlConfig;

    $curlC = $curlConfig;

    $curlC[CURLOPT_URL] = $url;

    curl_setopt_array($ch, $curlC);

    $result = curl_exec($ch);

    if (curl_errno($ch)) {
        echo 'Błąd #' . curl_errno($ch) . ': ' . curl_error($ch);
        return;
    }

    return $result;
}

function upload($url, $post) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    $response = curl_exec($ch);
}

function uploadFtp($fileName) {
    $ch = curl_init();
    $localfile = BASE_PATH . '/' . $fileName;
    $fp = fopen($localfile, 'r');

    curl_setopt($ch, CURLOPT_URL, 'ftp://admin@phpdev.c0.pl:QWerty0123@websearchboot.phpdev.c0.pl/' . $localfile);
    curl_setopt($ch, CURLOPT_UPLOAD, 1);
    curl_setopt($ch, CURLOPT_INFILE, $fp);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));

    curl_exec($ch);
    $error_no = curl_errno($ch);
    curl_close($ch);

    if ($error_no == 0) {
        $message = 'File uploaded successfully.';
    } else {
        $message = 'File upload error: ' . $error_no . '. Error codes explained here http://curl.haxx.se/libcurl/c/libcurl-errors.html';
    }
    echo $message;
}

function getInfo() {
    global $ch;
    $info = curl_getinfo($ch);
    return $info;
}

function getDir($url) {

    $s = preg_replace(
            array(
        '#^\.#',
        '#\?[^\/]+#',
        '#http://[^/]+#',
        '#/[^/]+\.[^/]+$#',
            ), array('', '', '', '/',)
            , $url);

    if (preg_match('#/$#', $s) == 0)
        $s = $s . '/';

    $s = substr($s, 0, strripos($s, "/"));

    return $s . '/';
}

function aLink($dom, $list, $attrib = 'href') {
    global $mysqli, $url, $tmpdbname, $force, $urlConf, $info, $content_type, $runHash;   

    $href = '';
    $d = '/';
    $robots = $dom->xpath && $dom->xpath->query('//meta[@name="robots"]')->length > 0 ? $dom->xpath->query('//meta[@name="robots"]')->item(0)->getAttribute('content') : '';
    $isOuterUrl = false;
    
    foreach ($list as $item) {

        $d = '/';

        $href = filterUrl($item->getAttribute($attrib), $urlConf['host']);
        
        if (preg_match('#^\./|^\?#', $item->getAttribute($attrib)) == 1) {
            $d = getDir($url);
        }
        
        
        if (preg_match('#^http(s?)://.*#', $href) == 1) {

            $isOuterUrl = true;

            if (!in_array(md5($item->getAttribute($attrib)), $runHash)) {
                //echo 'start "" /b php '. realpath(dirname(__FILE__)).'\..\getpost.php -u "' .$item->getAttribute('href').'"'."\n";
                //exec('start "" /b php '. realpath(dirname(__FILE__)).'\..\getpost.php -u "' .$url.'"');
            }
        }

        $text = $item->textContent;
        $title = $item->hasAttribute('title') ? $item->getAttribute('title') : NULL;
        $rel = $item->hasAttribute('rel') ? $item->getAttribute('rel') : NULL;

        if (
                preg_match('#noindex|nofollow#', $rel) == 1 ||
                preg_match('#noindex|nofollow#', $robots) == 1
        ) {
            continue;
        }

        
        $hrefOut = !$isOuterUrl ? $urlConf['scheme'] . '://' . $urlConf['host'] . '' . $d . $href : $href;

        $result = $mysqli->query("SELECT id, count(*) as c FROM tmp$tmpdbname where url like '$hrefOut'");
        //printf("\n$href\t{$item->getAttribute('href')}");
        $rowFind = $result ? $result->fetch_object() : null;

        if ($rowFind && $rowFind->c == 0) {
            $prefilter = '#\n|\r#';
            $inr = $mysqli->query("INSERT INTO tmp$tmpdbname VALUES (
                NULL,
                '" . md5($hrefOut) . "',
                '" . $hrefOut . "',
                '" . filter($title, $prefilter) . "',
                '" . filter($text, $prefilter) . "',    
                '" . filter($rel, $prefilter) . "',
                '" . filter($item->getAttribute('href'), $prefilter) . "',
                '" . filter($url, $prefilter) . "',NULL,NULL,NULL,
                '" . date("Y-m-d H:i:s", time()) . "')");
        } elseif ($rowFind && $rowFind->c == 1) {
            $inr = $mysqli->query("UPDATE tmp$tmpdbname set 
                title=CONCAT(title,';'," . $title . "'),
                text=CONCAT(text,';', '" . $text . "') where  id={$rowFind->id}");
        }

        $isOuterUrl = false;
    }
}
